package com.jintian.smart.kernel.flowable.domain.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAlias;

import com.jintian.smart.kernel.core.flow.IFlow;
import com.jintian.smart.kernel.orm.impl.BizTableImpl;
import lombok.Data;

@Data
public class FlowImpl implements IFlow {
    private String formUrl;
    private Integer silence;
    private String bizEntity;
    private List<BizRuleImpl> bizRules = new ArrayList<>();
    private List<BizTableImpl> bizTables = new ArrayList<>();
    @JsonAlias({"variables", "contextVariables"})
    private Map<String, String> contextVariables = new HashMap<>();
    private Map<String, String> taskAttributes = new HashMap<>();
    private List<ActivityImpl> actvities = new ArrayList<>();
    private List<StartRuleImpl> startRules = new ArrayList<>();

    @Override
    public ActivityImpl findActivity(String name) {
        for (ActivityImpl activity : actvities) {
            if (activity.getName().equals(name)) {
                return activity;
            }
        }
        return null;
    }

    public BizRuleImpl getBizRule(String name) {
        for (BizRuleImpl rule : bizRules) {
            if (rule.getName().equals(name)) {
                return rule;
            }
        }
        throw new IllegalArgumentException("不存在名称为" + name + "的业务规则");
    }

    public List<BizRuleImpl> getBizRulesByEvent(String event) {
        List<BizRuleImpl> ret = new ArrayList<>();
        for (BizRuleImpl rule : bizRules) {
            if (rule.getExcludeEvents() != null && rule.getExcludeEvents().contains(event)) {
                continue;
            }
            if (rule.getIncludeEvents() != null && rule.getIncludeEvents().contains(event)) {
                ret.add(rule);
            }
        }
        return ret;
    }
}
